<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-breadcrumb separator-class="el-icon-arrow-right" v-if="flag">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{this.dataSub.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>标签详情</el-breadcrumb-item>
        </el-breadcrumb>
        <el-form :inline="true" ref="form" :model="form" :rules="rules">
          <el-form-item label="标签名称"
            ><el-input
              v-model="tagName"
              style="width: 192px"
              size="mini"
            ></el-input
          ></el-form-item>
          <el-form-item class="second" label="状态">
            <el-select
              v-model="state"
              placeholder="请选择"
              style="width: 192px"
              size="mini"
            >
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="clear">清除</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="search"
              >搜索</el-button
            >
          </el-form-item>
          <el-form-item style="float: right">
            <el-button type="text" class="el-icon-back" v-if="flag" @click="back"
              >返回学科</el-button
            >
            <el-button size="small" type="success" @click="change('add')"
              ><i class="el-icon-edit"></i>新增标签</el-button
            >
          </el-form-item>
          <el-dialog
            :title="dialogTitle"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose"
          >
            <span>
              <el-form-item label="所属学科" prop="subjectID" v-if="flags"
                ><el-select
                  placeholder="请选择"
                  v-model="form.subjectID"
                  style="width: 250px"
                  size="mini"
                >

                  <el-option
                    v-for="item in datlist"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                  <el-option label="禁用" value="0"></el-option> </el-select
              ></el-form-item>
              <el-form-item label="目录名称" prop="tagName"
                ><el-input
                  v-model="form.tagName"
                  placeholder="请输入目录名称"
                  style="width: 250px"
                  size="mini"
                ></el-input>
              </el-form-item>
            </span>
            <span slot="footer" class="dialog-footer">
              <el-button @click="handleClose">取 消</el-button>
              <el-button type="primary" @click="submit">确 定</el-button>
            </span>
          </el-dialog>
        </el-form>

        <div class="date">
          <span><i class="el-icon-info"></i>数据一共{{ counts }}条</span>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" prop="id" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="164">
          </el-table-column>
          <el-table-column prop="tagName" label="标签名称" width="162">
          </el-table-column>
          <el-table-column prop="username" label="创建者" width="162">
          </el-table-column>
          <el-table-column label="创建日期" width="162">
            <template slot-scope="scope">
              {{ scope.row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态" width="120">
            <template slot-scope="scope">
              {{ scope.row.state ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="doAllow(row)">
                <template #default v-if="row.state === 1"> 禁用 </template>
                <template #default v-else>启用 </template>
              </el-button>
              <el-button
                type="text"
                size="small"
                @click="change('edit', row.id)"
                :disabled="row.state === 1"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="del(row.id)"
                :disabled="row.state === 1"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 20px; text-align: right">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="10"
            layout=" prev, pager, next, sizes, jumper"
            :total="counts"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import {
  list,
  remove,
  changeState,
  add,
  detail,
  update
} from '@/api/hmmm/tags'
export default {
  data () {
    return {
      flag: false,
      dataSub: {
        name: this.$route.query.subjectName,
        id: this.$route.query.id
      },
      state: null,
      tagName: '',
      page: 1,
      pagesize: 10,
      counts: 0,
      tableData: [],
      dialogType: 'add', // 默认是新增
      dialogVisible: false,
      datlist: '',
      form: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        subjectID: [{ required: true, message: '请选择所属学科' }],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    subName (name) {
      if (name) {
        this.tagName = this.$route.query.subjectName
        this.search()
      }
    }
  },
  created () {
    this.getList()
    this.getnewtab()
    this.subName()
    this.search()
  },
  methods: {
    subName () {
      if (this.$route.query.subjectName !== undefined) {
        this.flag = true
        this.tagName = this.$route.query.subjectName
      }
      this.search()
    },
    // 返回学科列表
    back () {
      this.$router.push('list')
    },
    async getList () {
      const res = await list({
        tagName: this.tagName,
        page: this.page,
        pagesize: this.pagesize,
        state: this.state
      })
      console.log(res)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    // 新增确认
    async submit () {
      await this.$refs.form.validate()
      // console.log(123)
      // 根据type判断是添加还是修改
      if (this.dialogType === 'add') {
        await add(this.form)
        this.$message.success('操作成功')
      }
      if (this.dialogType === 'edit') {
        const { subjectID, tagName, id } = this.form
        await update({ subjectID, tagName, id })
        this.$message.success('操作成功')
      }
      // this.dialogVisible = false
      // this.$refs.form.resetFields()
      this.closeDia()
      this.page = 1
      this.getList()
    },
    closeDia () {
      this.dialogVisible = false
      // 冗余数据清空
      this.form = {
        subjectID: '',
        tagName: ''
      }
      this.$refs.form.resetFields()
    },
    async getnewtab () {
      const res = await simple()
      this.datlist = res.data
      console.log(res)
    },
    // 搜索
    search () {
      this.page = 1
      this.getList()
    },
    // 清除
    clear () {
      console.log(this.dataSub)
      this.tagName = ''
      this.state = null
      this.page = 1
    },
    // 新增,修改同一按钮
    async change (type, id, name) {
      console.log(type, id)
      this.dialogType = type
      this.dialogVisible = true
      if (type !== 'add') {
        const res = await detail({ id })
        console.log(res)
        this.form = {
          ...res.data
        }
        // this.form.subjectID = res.data.subjectID
        // this.form.tagName = res.data.tagName
      }
    },
    // 删除
    del (id) {
      console.log(id, '文章')
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.$message.success('删除成功')
        this.getList()
      })
    },
    // 切换页码数量
    handleSizeChange (val) {
      console.log(val)
      this.page = 1
      this.pagesize = val
      this.getList()
    },
    // 切换页
    handleCurrentChange (val) {
      console.log(val)
      this.page = val
      this.getList()
    },
    // 点击启用按钮
    async doAllow (row) {
      console.log(row)
      // 取反
      const state = row.state ? 0 : 1
      // 发送请求更改数据
      await changeState({ state, id: row.id })
      this.$message.success('操作成功')
      // 重新渲染数据
      this.getList()
    },
    handleClose () {
      this.closeDia()
    }
  },
  computed: {
    dialogTitle () {
      let title = ''
      if (this.dialogType === 'add') title = '新增目录'
      if (this.dialogType === 'edit') title = '修改目录'
      return title
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  // .app-container {
  //   // padding: 0px;
  // }
  ::v-deep .el-table th {
    background-color: #fafafa;
  }
  .el-input {
    height: 32px;
  }
  .second {
    margin-left: 30px;
  }
  .element.style {
    height: 250px;
  }
  ::v-deep .el-dialog__footer {
    text-align: right;
  }
  .date {
    margin-bottom: 15px;
    background-color: #f4f4f5;
    padding: 8px 16px;
    border-radius: 4px;
    i {
      color: #909399;
      margin-right: 8px;
    }
    span {
      // padding: 0 8px;
      color: #909399;
      font-size: 14px;
    }
  }
}
.el-link.el-link--success {
  margin-right: 10px;
  color: #409eff;
}
.el-icon-back {
  font-size: 14px;
  color: #409eff;
  line-height: 10px;
}
.el-breadcrumb {
  margin-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
  height: 30px;
}
.app-container {
  padding: 10px;
}
</style>
